﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DN100UserInfo : MonoBehaviour {


    public UISprite avatar;
    public UILabel userNameLable;
    public UILabel userID;
    public UILabel userCoin;
    public int uid;
    public UITexture wxAvatar;
    public GameObject BackBtn;
    private void Awake()
    {
        UIEventListener.Get(BackBtn).onClick = ButtonClick;
    }

    void Start()
    {
        //LoadUserInfo(uid);
    }

    void ButtonClick(GameObject button)
    {
        if (button.name == "BackBtn")
        {
            Destroy(gameObject);
        }
    }

    public void LoadUserInfo(int uid)
    {
        EginUser user = EginUser.Instance;
        //EginDN dn = EginDN.Instance;
        for (int i = 0; i < user.gameUserInfoList.Count; i++)
        {
            if (user.gameUserInfoList[i].Uid == uid)
            {
                if(user.gameUserInfoList[i].AvatarUrl != ""){
                    WXAvatarManager._instance.GetWxAvatar(uid,wxAvatar,user.gameUserInfoList[i].AvatarUrl);
                    wxAvatar.gameObject.SetActive(true);
                }
                else
                {
                    avatar.spriteName = user.gameUserInfoList[i].AvatarId.ToString();                  
                }
                userNameLable.text = user.gameUserInfoList[i].Nickname;
                userID.text = user.gameUserInfoList[i].Uid.ToString();
                int coin = user.gameUserInfoList[i].BagCoinCount;
                userCoin.text = user.ReturnMoney(coin);
                Debug.Log(user.gameUserInfoList[i].Uid + "   " + user.gameUserInfoList[i].BagCoinCount);

                //StartCoroutine(GetWWWFACE(wxAvatar, user.gameUserInfoList[i].AvatarUrl));
            }
        }

    }

    /// <summary>
    /// 加载微信头像
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    IEnumerator GetWWWFACE(UITexture texture, string url)
    {

        if (url != "")
        {
            WWW www = new WWW(url);


            yield return www;
            if (www.isDone)
            {
                if (www.error == null) { }
                texture.mainTexture = www.texture;

            }
            else
            {

            }
            www.Dispose();
        }

    }
}
